package com.longtop.perform.zb.index.web;

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.zb.index.service.IndexCustomService;

/**
 * 指标管理--复合指标Action
 * 
 * @author 薛延明
 * 
 */
public class IndexCustomListAction extends BaseAction {

	private final static String DATASET_INDEX = "Dataset_Index";
	// 复合指标Service
	private IndexCustomService indexCustomService = getBean(IndexCustomService.BEAN_ID);

	/**
	 * 系统共用：初始化调用
	 * 
	 * @param context
	 */
	public void init(IInitContext context) {
		
		super.init(context);
	}

	/**
	 * 初始化业务数据(覆写父类方法)
	 * 
	 * @param context
	 */
	public void thisPageInit(IInitContext context) {
		
		commonQueryIndexCustomList(context, false);
	}
	
	/**
	 * 条件查询  (根据复合指标ID和复合指标名称)
	 * 
	 * @param context
	 */
	public void searchIndexCustomList(IActionContext context) {
		
		commonQueryIndexCustomList(context, true);
	}
	
	/**
	 * 翻页查询
	 * 
	 * @param context
	 */
	public void loadIndexCustomList(IActionContext context) {

		commonQueryIndexCustomList(context, false);
	}
	
	
	/**
	 * 复合指标查询
	 * 
	 * @param object  (IInitContext或IActionContext)
	 * @param isFirstPageNo  是否从第一页开始查询
	 */
	public void commonQueryIndexCustomList(Object object, boolean isFirstPageNo){
		
		// 取得复合指标Dataset
		Dataset dataset = this.getDataset(object, DATASET_INDEX);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_INDEX)) {
			return;
		}
		// 指标ID, 指标名称
		String indexId = "", indexName = "";
		// 判断object对象类型是否是IActionContext
		if(object instanceof IActionContext){
			IActionContext context = (IActionContext) object;
			indexId = context.getElementValue("Text_indexId");
			indexName = context.getElementValue("Text_indexName");
		}
		
		// 设置参数
		Map<String, String> params = new HashMap<String, String>();
		params.put("indexId", indexId);
		params.put("indexName", indexName);
		// 当前开始页数
		int pageNo = 1;
		if (!isFirstPageNo){
			pageNo = dataset.getPageNo();
		}
		QueryResult queryResult = null;
		try {
			// 查询复合指标列表记录
			queryResult = indexCustomService.searchIndexCustomList(params, pageNo, dataset.getPageSize());
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		}
		BeanUtil.queryResultToDataset(queryResult, dataset, false, true);
	}

	/**
	 * 删除复合指标
	 * 
	 * @param context
	 */
	public void deleteIndexCustom(IActionContext context) {
		
		// 取得复合指标ID
		String indexId = context.getCommandParameterValue("indexId");
		try {
			indexCustomService.deleteIndexCustom(indexId);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		}
	}
}